package com.the4man.framework.tag;

import com.the4man.framework.cache.PermissionCache;
import com.the4man.framework.pagemodel.PUser;
import com.the4man.framework.utils.RequestUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;
import java.util.HashSet;

/**
 * Created by lenovo on 2017/12/12.
 */
public class AuthButton extends BodyTagSupport {
    private static final Logger LOGGER = LoggerFactory.getLogger(AuthButton.class);
    private PermissionCache permissionCache;
    private String authUrl;
    private String authKey;
    private String id;
    private String name;
    private String text;
    private String value;
    private String cssClass;
    private String property;

    public AuthButton() {
    }

    public int doAfterBody() throws JspException {
        BodyContent bc = this.getBodyContent();
        JspWriter out = this.getPreviousOut();

        try {
            out.write(bc.getString());
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return 0;
    }

    public int doStartTag() throws JspException {
        PUser user = RequestUtil.getSessionUserinfo();
        WebApplicationContext context = (WebApplicationContext)this.pageContext.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
        this.permissionCache = (PermissionCache)context.getBean("permissionCache");

        try {
            if(user == null) {
                return 0;
            }

            HashSet<String> needToVerifyPermissions = this.permissionCache.getNeedToVerifyPermissions();
            String suri = StringUtils.startsWith(this.authUrl, "/")?StringUtils.substringAfter(this.authUrl, "/"):this.authUrl;
            String cnbdwStart = StringUtils.startsWith(suri, "cnbdw")?suri:"cnbdw/" + suri;
            String cnbdwStart1 = "/" + cnbdwStart;
            String start = StringUtils.startsWith(suri, "cnbdw")?StringUtils.substringAfter(cnbdwStart, "cnbdw/"):suri;
            String start1 = "/" + start;
            if(needToVerifyPermissions == null || needToVerifyPermissions.isEmpty() || !needToVerifyPermissions.contains(cnbdwStart) && !needToVerifyPermissions.contains(cnbdwStart1) && !needToVerifyPermissions.contains(start) && !needToVerifyPermissions.contains(start1)) {
                return 6;
            }

            if(needToVerifyPermissions != null && !needToVerifyPermissions.isEmpty() && (needToVerifyPermissions.contains(cnbdwStart) || needToVerifyPermissions.contains(cnbdwStart1) || needToVerifyPermissions.contains(start) || needToVerifyPermissions.contains(start1))) {
                this.permissionCache = (PermissionCache)context.getBean("permissionCache");
                HashSet<String> permissions = this.permissionCache.getPermissionByRoleId(user.getRoleid());
                if(permissions != null && !permissions.isEmpty()) {
                    boolean isAuth = false;
                    if(!org.apache.commons.lang3.StringUtils.isBlank(this.authUrl)) {
                        String[] urls = this.authUrl.split(";");
                        String[] var15 = urls;
                        int var14 = urls.length;

                        for(int var13 = 0; var13 < var14; ++var13) {
                            String var10000 = var15[var13];
                            if(permissions.contains(cnbdwStart) || permissions.contains(cnbdwStart1) || permissions.contains(start) || permissions.contains(start1)) {
                                isAuth = true;
                                break;
                            }
                        }
                    }

                    if(isAuth) {
                        return 6;
                    }

                    LOGGER.info(this.authUrl + "未被授权，" + this.text + "按钮不显示");
                    return 0;
                }

                return 0;
            }

            return 6;
        } catch (NullPointerException var16) {
            var16.printStackTrace();
        } catch (Exception var17) {
            var17.printStackTrace();
        }

        return 0;
    }

    private void writeHtml() {
        JspWriter out = this.pageContext.getOut();

        try {
            this.replaceELExpression(this.text);
            out.println(this.text);
        } catch (Exception var3) {
            var3.printStackTrace();
        }

    }

    public int doEndTag() throws JspException {
        return 6;
    }

    private void replaceELExpression(String value) {
        if(!StringUtils.isBlank(value)) {
            String[] els = StringUtils.substringsBetween(value, "${", "}");
            if(els != null && els.length != 0) {
                String[] var6 = els;
                int var5 = els.length;

                for(int var4 = 0; var4 < var5; ++var4) {
                    String el = var6[var4];
                    Object attribute = this.getAttribute(el);
                    value = value.replaceAll("${" + el + "}", attribute.toString());
                }

            }
        }
    }

    private Object getAttribute(String el) {
        Object attribute = this.pageContext.getAttribute(el);
        if(attribute != null) {
            return attribute;
        } else {
            attribute = this.pageContext.getRequest().getAttribute(el);
            if(attribute != null) {
                return attribute;
            } else {
                attribute = this.pageContext.getSession().getAttribute(el);
                if(attribute != null) {
                    return attribute;
                } else {
                    attribute = this.pageContext.getServletContext().getAttribute(el);
                    return attribute != null?attribute:"";
                }
            }
        }
    }

    public String getAuthUrl() {
        return this.authUrl;
    }

    public void setAuthUrl(String authUrl) {
        this.authUrl = authUrl;
    }

    public String getAuthKey() {
        return this.authKey;
    }

    public void setAuthKey(String authKey) {
        this.authKey = authKey;
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCssClass() {
        return this.cssClass;
    }

    public void setCssClass(String cssClass) {
        this.cssClass = cssClass;
    }

    public String getText() {
        return this.text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getProperty() {
        return this.property;
    }

    public void setProperty(String property) {
        this.property = property;
    }

    public String getValue() {
        return this.value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
